package com.learning.jquizenglish.screen.doquiz;

import java.util.ArrayList;

import android.media.MediaPlayer;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.learning.jquizenglish.JQuizAppConfig;
import com.learning.jquizenglish.R;
import com.learning.jquizenglish.activity.DoQuizActivity;
import com.learning.jquizenglish.common.controller.AudioStreamingController;
import com.learning.jquizenglish.model.JQuizObserver;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.screen.doquiz.DoQuizScreen.DoQuizMode;

// TODO: Auto-generated Javadoc
/**
 * The Class DoQuizView.
 */
public class DoQuizView {

	/** The m view pager. */
	private ViewPager mViewPager;
	
	/** The btn play stop. */
	private Button	btnPlayStop;
	
	/** The btn finish. */
	private ImageButton btnFinish;
	
	/** The txt indicator. */
	private TextView	txtIndicator;
	
	/** The m frag activity. */
	private FragmentActivity mFragActivity;
	
	/** The m do quiz screen. */
	private DoQuizScreen mDoQuizScreen;
	
	/** The m li question models. */
	private ArrayList<QuestionModel> mLiQuestionModels;
	
	public AudioStreamingController mediaController;
	
	public MediaPlayer mediaPlayer;
	/**
	 * Instantiates a new do quiz view.
	 *
	 * @param fragScreen the frag screen
	 */
	public DoQuizView(DoQuizScreen fragScreen) {
		mDoQuizScreen = fragScreen;
	}
	
	/**
	 * Inits the.
	 *
	 * @param fragActivity the frag activity
	 * @param contentView the content view
	 */
	public void init(FragmentActivity fragActivity,View contentView){
		mFragActivity = fragActivity;
		mViewPager = (ViewPager) contentView.findViewById(R.id.pager);
		btnPlayStop	= (Button) contentView.findViewById(R.id.btnPlayStop);
		btnFinish	= (ImageButton) contentView.findViewById(R.id.btn_finish);
		txtIndicator = (TextView) contentView.findViewById(R.id.tv_indicator);
		initData();
	}
	
	/**
	 * Inits the data.
	 */
	private void initData(){
		mLiQuestionModels = DoQuizSession.getInstance().getListQuestions();
		if(mLiQuestionModels==null ||mLiQuestionModels.size()==0)
			return;
		txtIndicator.setText(Html.fromHtml(getPageIndicator(1)));
		QuestionSlideAdapter questionSlideAdapter = new QuestionSlideAdapter(mFragActivity.getSupportFragmentManager());
		mViewPager.setAdapter(questionSlideAdapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int pos) {
				txtIndicator.setText(Html.fromHtml(getPageIndicator(pos+1)));
				if(pos == (mLiQuestionModels.size()-1)){
					btnFinish.setVisibility(View.VISIBLE);
				}else{
					btnFinish.setVisibility(View.GONE);
				}
				if (JQuizAppConfig.IS_LISTENING_APP) {
					((DoQuizActivity)mFragActivity).setAudioUrl(JQuizEnglishApi.BASE_AUDIO_URL+mLiQuestionModels.get(pos).audio);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				
			}
		});
		btnFinish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!DoQuizSession.getInstance().isAnswerAllQuestion()) {
					Toast.makeText(mFragActivity, "You have not answer all questions", Toast.LENGTH_SHORT).show();
					return;
				}
				mDoQuizScreen.changeMode(DoQuizMode.RESULT);
			}
		});
		if(JQuizAppConfig.IS_LISTENING_APP){
			btnPlayStop.setVisibility(View.VISIBLE);
			initialAudioPlayer();
		}else{
			btnPlayStop.setVisibility(View.GONE);
		}
	}
	
	/**
	 * Initial audio player.
	 */
	private void initialAudioPlayer(){
		((DoQuizActivity)mFragActivity).setButtonController(btnPlayStop);
		((DoQuizActivity)mFragActivity).setAudioUrl(JQuizEnglishApi.BASE_AUDIO_URL+mLiQuestionModels.get(0).audio);
	}
	
	
	/**
	 * On destroy.
	 */
	public void onDestroy(){
		
	}
	
	/**
	 * Gets the current question.
	 *
	 * @return the current question
	 */
	public QuestionModel getCurrentQuestion(){
		return mLiQuestionModels.get(mViewPager.getCurrentItem());
	}
	
	/**
	 * Gets the page indicator.
	 *
	 * @param questionNumber the question number
	 * @return the page indicator
	 */
	public String getPageIndicator(int questionNumber){
		String ret = "";
		String temp1 = "[";
		String temp2 = "]";
		int size = mLiQuestionModels.size();
		for (int i = 1; i <= size; i++) {
			if(i==questionNumber){
				if (mLiQuestionModels.get(i-1).isAnswered) {
					if (mLiQuestionModels.get(i-1).isCorrectAnswer()) {
						ret = ret + " <font color='#00ADEE'>" +temp1 + i + temp2+ "</font> ";
					}else{
						ret = ret + " <font color='#CD3F31'>" +temp1 + i + temp2+ "</font> ";
					}
				}else{
					ret = ret + " <font color='#000000'>" +temp1 + i + temp2+ "</font> ";
				}
			}else{
				if (mLiQuestionModels.get(i-1).isAnswered) {
					if (mLiQuestionModels.get(i-1).isCorrectAnswer()) {
						ret = ret + " <font color='#00ADEE'>" +i+ "</font> ";
					}else{
						ret = ret + " <font color='#CD3F31'>" +i+ "</font> ";
					}
				}else{
					ret = ret + " <font color='#000000'>" +i+ "</font> ";
				}
			}
		}
		return ret;
	}
	
	/**
	 * The Class QuestionSlideAdapter.
	 */
	public class QuestionSlideAdapter extends FragmentStatePagerAdapter{

		/**
		 * Instantiates a new question slide adapter.
		 *
		 * @param fm the fm
		 */
		public QuestionSlideAdapter(FragmentManager fm) {
			super(fm);
		}

		/* (non-Javadoc)
		 * @see android.support.v4.app.FragmentStatePagerAdapter#getItem(int)
		 */
		@Override
		public Fragment getItem(int position) {
			return QuestionFragment.create(position,new JQuizObserver() {
				
				@Override
				public void onUpdate(Object ob) {
					txtIndicator.setText(Html.fromHtml(getPageIndicator((Integer)ob+1)));
				}
			});
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.PagerAdapter#getCount()
		 */
		@Override
		public int getCount() {
			return mLiQuestionModels.size();
		}
		
	}
}
